import request from "../utils/request";

export interface SendMessageRequest {
  toUserId: number;
  content: string;
  contentType?: string;
}

export const messageApi = {
  // 发送私聊消息
  sendMessage: (data: SendMessageRequest) => {
    return request.post("/api/v1/message/send", data);
  },

  // 获取用户消息列表
  getUserMessages: (page: number = 1, size: number = 20) => {
    return request.get(`/api/v1/message/list?page=${page}&size=${size}`);
  },

  // 标记消息为已读
  markAsRead: (messageId: number) => {
    return request.post(`/api/v1/message/read/${messageId}`);
  },

  // 获取未读消息数量
  getUnreadCount: () => {
    return request.get("/api/v1/message/unread-count");
  },

  // 删除消息
  deleteMessage: (messageId: number) => {
    return request.delete(`/api/v1/message/${messageId}`);
  },

  // 搜索用户进行私聊
  searchUsers: (keyword: string, userType?: string) => {
    const params = new URLSearchParams({ keyword });
    if (userType) params.append("userType", userType);
    return request.get(`/api/v1/message/search-users?${params.toString()}`);
  },

  // 获取会话列表
  getConversations: () => {
    return request.get("/api/v1/message/conversations");
  },

  // 获取会话消息
  getConversationMessages: (
    conversationId: string,
    page: number = 1,
    size: number = 20
  ) => {
    return request.get(
      `/api/v1/message/conversations/${conversationId}/messages?page=${page}&size=${size}`
    );
  },

  // 获取AI智能建议
  getAISuggestion: (data: { content: string }) => {
    return request.post("/api/v1/message/ai-suggestion", data);
  },
};
